package com.business.api.user.controller;

import com.business.api.user.dto.UserDTO;
import com.business.api.user.from.UpdateUserInfoFrom;
import com.business.api.user.from.UserResetPasswordFrom;
import com.business.api.user.service.UserApiService;
import com.business.api.user.vo.UserVO;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.vo.SeedBaseVO;
import com.seed.util.EntityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author gchiaway
 * 日期: 2020-06-11
 * 时间: 12:57
 */

@Slf4j
@RestController(value = "userApiController")
@Api(value = "userController", tags = "用户信息管理")
@RequestMapping("/app/v1/user")
public class UserApiController {
    @Autowired
    private UserApiService userService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户信息")
    public UserVO detail() {
        UserVO result = new UserVO();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();

        if (ObjectUtils.isEmpty(userId)) {
            result.setSuccess(false);
            result.setCode("A0410");
            result.setMessage("id不能为空");
            return result;
        }

        UserDTO userDTO;
        try {
            userDTO = userService.getDetail(userId);
        } catch (ServiceException e) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage("获取用户信息出错");
            return result;
        }
        if (!userDTO.getSuccess()) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage(userDTO.getMessage());
            return result;
        }
        result.setData(userDTO.getDetailVO());
        return result;
    }

    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改密码")
    public SeedBaseVO resetPassword(@RequestBody @ApiParam(value = "更新表单", required = true) UserResetPasswordFrom userResetPasswordFrom) {
        log.info("[api-v1]userController.resetPassword,修改密码");
        SeedBaseVO result = new SeedBaseVO();

        if (ObjectUtils.isEmpty(userResetPasswordFrom)) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage("请求数据不能为空");
            return result;
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        userResetPasswordFrom.setUserId(sysUser.getId());
        try {
            if (EntityUtil.isAnyFieldNull(userResetPasswordFrom)) {
                result.setSuccess(false);
                result.setCode("A0500");
                result.setMessage("请求数据不能为空");
                return result;
            }
        } catch (Exception e) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage("请求数据异常");
            return result;
        }

        if (!userResetPasswordFrom.getNewPassword().equals(userResetPasswordFrom.getConfirmPassword())) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage("两次输入密码不一致");
            return result;
        }

        SeedMessageDTO serviceResult;
        try {
            serviceResult = userService.resetPassword(userResetPasswordFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]userController.resetPassword ", e);
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage("修改失败");
            return result;
        }

        if (!serviceResult.getSuccess()) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage(serviceResult.getMessage());
            return result;
        }

        return result;
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "完全更新,更新from中所有的值（空值也会被更新）")
    public UserVO update(@RequestBody @ApiParam(value = "更新表单", required = true) UpdateUserInfoFrom userInfoFrom) {
        log.info("[api-v1]userController.update,完全更新");
        UserVO result = new UserVO();
        if (ObjectUtils.isEmpty(userInfoFrom)) {
            result.setSuccess(false);
            result.setCode("A0410");
            result.setMessage("请求数据不能为空");
            return result;
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        userInfoFrom.setId(sysUser.getId());
        if (ObjectUtils.isEmpty(userInfoFrom.getId())) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage("异常的用户信息,当前登录用户为空.");
            return result;
        }
        UserDTO userDTO;
        try {
            userDTO = userService.update(userInfoFrom);
        } catch (ServiceException e) {
            result.setSuccess(false);
            result.setCode("A0500");
            result.setMessage("获取用户信息出错");
            return result;
        }
        if (!userDTO.getSuccess()) {
            result.setSuccess(false);
            result.setMessage(userDTO.getMessage());
            return result;
        }
        result.setData(userDTO.getDetailVO());
        return result;
    }

}
